package summerprojectFX.controller;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import summerprojectFX.model.Song;
import summerprojectFX.parser.DOCFile;
import summerprojectFX.parser.PDFFile;
import summerprojectFX.parser.TextFile;
import summerprojectFX.view.MainView;

public class SaveAs {

	private File file;
	private Song song;

	public SaveAs(MainView view, Stage primary) {
		song = view.getSelectedSong();
		FileChooser fileChooser = new FileChooser();

		// Set extension filter
		FileChooser.ExtensionFilter extFilter1 = new FileChooser.ExtensionFilter(
				"TXT files (*.txt)", "*.txt");
		fileChooser.getExtensionFilters().add(extFilter1);
		

		file = fileChooser.showSaveDialog(primary);

		if (file != null && song != null) {
			handleSaveAs(view);
		}
	}

	private void handleSaveAs(MainView view) {

		try {
                                                
                                                     

			TextFile tfr = new TextFile(file);
			tfr.write(song, file.getAbsolutePath() + ".txt");

		} catch (Exception e) {
			System.out.println("Error writing txt file.");
		}

	}
}
